<?php
namespace my;
use baidu\ocr\AipOcr;

class BaiduOcr {
    private $app_id;
    private $api_key;
    private $secret_key;
    private $client = null;

    public function __construct($config = []) {
        $this->app_id = env('baidu.app_id','');
        $this->api_key = env('baidu.api_key','');
        $this->secret_key = env('baidu.secret_key','');
        $this->client = new AipOcr($this->app_id, $this->api_key, $this->secret_key);
    }

    // 识别身份证号
    public function getIdCardNumber($file_path,$side) {
        $image = file_get_contents($file_path);
        return $this->client->idcard($image,$side);
    }

    // 图片中读取文字
    public function getTextFromImage($file_path, $file_url='') {
        $image = file_get_contents($file_path);
        // 调用通用文字识别, 图片参数为本地图片
        return $this->client->basicGeneral($image);
//        return $this->client->basicAccurate($image);
//        // 如果有可选参数
//        $options = array();
//        $options["language_type"] = "CHN_ENG";
//        $options["detect_direction"] = "true";
//        $options["detect_language"] = "true";
//        $options["probability"] = "true";
//
//        // 带参数调用通用文字识别, 图片参数为本地图片
//        $client->basicGeneral($image, $options);
//        $url = "https//www.x.com/sample.jpg";
//
//        // 调用通用文字识别, 图片参数为远程url图片
//        $client->basicGeneralUrl($url);
//
//        // 如果有可选参数
//        $options = array();
//        $options["language_type"] = "CHN_ENG";
//        $options["detect_direction"] = "true";
//        $options["detect_language"] = "true";
//        $options["probability"] = "true";
//
//        // 带参数调用通用文字识别, 图片参数为远程url图片
//        $client->basicGeneralUrl($url, $options);
    }

    public function getTextFromPdf() {
        $pdf_file = file_get_contents('./test.pdf');
        // 调用办公文档识别
        return $this->client->docAnalysisOfficePdf($pdf_file,[
            'pdf_file_num' => 1, // 第几页
            'words_type' => 'handwring_only',
        ]);
    }

}